<?php

namespace Model\Factory;

use Core\Db\Feature\InjectMetaFieldFeature;
use Core\Db\Feature\LightMetaFeature;
use Model\Address;
use Model\AddressTable;
use Model\Album;
use Model\Category;
use Model\CategoryTable;
use Model\Dish;
use Model\DishArticle;
use Model\DishArticleTable;
use Model\DishCategory;
use Model\DishCategoryTable;
use Model\DishImage;
use Model\DishImageTable;
use Model\DishLang;
use Model\DishLangTable;
use Model\DishPrice;
use Model\DishPriceTable;
use Model\DishTable;
use Model\DishTag;
use Model\DishTagTable;
use Model\District;
use Model\DistrictTable;
use Model\DomainConfig;
use Model\DomainConfigTable;
use Model\DomainPage;
use Model\DomainPageTable;
use Model\DomainTemplate;
use Model\DomainTemplateGroup;
use Model\DomainTemplateGroupTable;
use Model\DomainTemplateTable;
use Model\DomainUser;
use Model\DomainUserTable;
use Model\Province;
use Model\ProvinceTable;
use Model\Restaurant;
use Model\RestaurantOwner;
use Model\RestaurantOwnerTable;
use Model\RestaurantTable;
use Model\Tag;
use Model\ArticleTag;
use Model\ArticleCategory;
use Model\Article;
use Model\AlbumTable;
use Model\ArticleCategoryTable;
use Model\ArticleTable;
use Model\BlockClass;
use Model\BlockClassTable;
use Model\BlockInstance;
use Model\BlockInstanceTable;
use Model\CountryTable;
use Model\Menu;
use Model\MenuItem;
use Model\MenuItemTable;
use Model\MenuTable;
use Model\Page;
use Model\PageBlock;
use Model\PageBlockTable;
use Model\PageTable;
use Model\Privilege;
use Model\PrivilegeTable;
use Model\PrivilegeType;
use Model\PrivilegeTypeTable;
use Model\Role;
use Model\RolePrivilegeTable;
use Model\RoleTable;
use Model\Route;
use Model\RouteTable;
use Model\Template;
use Model\TemplateBlock;
use Model\TemplateBlockTable;
use Model\TemplateTable;
use Model\Unit;
use Model\UnitLang;
use Model\UnitLangTable;
use Model\UnitTable;
use Model\User;
use Model\UserBalance;
use Model\UserBalanceTable;
use Model\UserRole;
use Model\UserRoleTable;
use Model\UserTable;
use Model\TagTable;
use Model\ArticleTagTable;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;

use Zend\Db\TableGateway\Feature\FeatureSet;
use Zend\Db\TableGateway\Feature\MetadataFeature;
use Zend\Db\TableGateway\TableGateway;

/**
 * Class TableGatewayFactory
 * @package Model\Factory
 * @description use the class in case of cannot get service locator because the class do not follow DI principle
 */

class TableGatewayFactory{

    /**
     * @var Adapter
     */
    private static  $_defaultDbAdapter;


    /**
     * @return Adapter
     */
    public static function getDefaultAdapter(){
        if(!self::$_defaultDbAdapter){
            $modelModuleConfig = include MODULE_PATH.'/Model/config/module.config.php';
            self::$_defaultDbAdapter = new Adapter($modelModuleConfig['db']);
        }
        return self::$_defaultDbAdapter;
    }

    /**
     * @param \Zend\Db\Adapter\Adapter $defaultDbAdpter
     */
    public static function setDbAdapter($dbAdapter)
    {
        self::$_defaultDbAdapter = $dbAdapter;
    }


    public static function getDbAdapter()
    {
        if (!self::$_defaultDbAdapter)
            return self::getDefaultAdapter();
        else
            return self::$_defaultDbAdapter;
    }


    /**
     * @param $tableName
     * @param $entityClass
     * @return TableGateway
     */
    public static function getGenericTableGateway($tableName, $entityClass){
        $dbAdapter = self::getDbAdapter();
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype($entityClass);

        $featureSet = new FeatureSet();
        $featureSet->addFeature(new LightMetaFeature());
        $featureSet->addFeature(new InjectMetaFieldFeature());
//        $featureSet->addFeature(new MetadataFeature());

        return new TableGateway($tableName, $dbAdapter, $featureSet, $resultSetPrototype);
    }

    /**
     * @return UserTable
     */
    public static function getUserTable(){
        $table = new UserTable();
        $table->setTableGateway(self::getGenericTableGateway('user', new User()));
        return $table;

    }

    /**
     * @return CountryTable
     */
    public static function getCountryTable(){
        $table = new CountryTable();
        $table->setTableGateway(self::getGenericTableGateway('country', new CountryTable()));
        return $table;

    }

    /**
     * @return AlbumTable
     */
    public static function getAlbumTable(){
        $table = new AlbumTable();
        $table->setTableGateway(self::getGenericTableGateway('album', new Album()));
        return $table;

    }

    /**
     * @return BlockClassTable
     */
    public static function getBlockClassTable(){
        $table = new BlockClassTable();
        $table->setTableGateway(self::getGenericTableGateway('block_class', new BlockClass()));
        return $table;

    }

    /**
     * @return BlockInstanceTable
     */
    public static function getBlockInstanceTable(){
        $table = new BlockInstanceTable();
        $table->setTableGateway(self::getGenericTableGateway('block_instance', new BlockInstance()));
        return $table;
    }

    /**
     * @return TemplateTable
     */
    public static function getTemplateTable($lang){
        $table = new TemplateTable();
        $table->setTableGateway(self::getGenericTableGateway('template', new Template()));
        $table->setLang($lang);
        return $table;

    }

    /**
     * @return TemplateBlockTable
     */
    public static function getTemplateBlockTable(){
        $table = new TemplateBlockTable();
        $table->setTableGateway(self::getGenericTableGateway('template_block', new TemplateBlock()));

        return $table;

    }

    /**
     * @return PrivilegeTable
     */
    public static function getPrivilegeTable(){
        $table = new PrivilegeTable();
        $table->setTableGateway(self::getGenericTableGateway('privilege', new Privilege()));
        return $table;
    }

    /**
     * @return PrivilegeTypeTable
     */
    public static function getPrivilegeTypeTable(){
        $table = new PrivilegeTypeTable();
        $table->setTableGateway(self::getGenericTableGateway('privilege_type', new PrivilegeType()));
        return $table;
    }

    /**
     * @return RoleTable
     */
    public static function getRoleTable(){
        $table = new RoleTable();
        $table->setTableGateway(self::getGenericTableGateway('role', new Role()));
        return $table;
    }


    /**
     * @return RolePrivilegeTable
     */
    public static function getRolePrivilegeTable(){
        $table = new RolePrivilegeTable();
        $table->setTableGateway(self::getGenericTableGateway('role_privilege', new Role()));
        return $table;
    }


    /**
     * @return UserRoleTable
     */
    public static function getUserRoleTable(){
        $table = new UserRoleTable();
        $table->setTableGateway(self::getGenericTableGateway('user_role', new UserRole()));
        return $table;
    }

    /**
     * @return PageTable
     */
    public static function getPageTable(){
        $table = new PageTable();
        $table->setTableGateway(self::getGenericTableGateway('page', new Page()));
        return $table;
    }


    /**
     * @return PageBlockTable
     */
    public static function getPageBlockTable(){
        $table = new PageBlockTable();
        $table->setTableGateway(self::getGenericTableGateway('page_block', new PageBlock()));
        return $table;
    }

    /**
     * @return RouteTable
     */
    public static function getRouteTable($lang){
        $table = new RouteTable();
        $table->setTableGateway(self::getGenericTableGateway('route', new Route()));
        $table->setLang($lang);
        return $table;
    }

    /**
     * @return MenuTable
     */
    public static function getMenuTable($lang){
        $table = new MenuTable();
        $table->setTableGateway(self::getGenericTableGateway('menu', new Menu()));
        $table->setLang($lang);
        return $table;
    }

    /**
     * @return MenuItemTable
     */
    public static function getMenuItemTable(){
        $table = new MenuItemTable();
        $table->setTableGateway(self::getGenericTableGateway('menu_item', new MenuItem()));
        return $table;
    }

    /**
     * @return ArticleTable
     */
    public static function getArticleTable(){
        $table = new ArticleTable();
        $table->setTableGateway(self::getGenericTableGateway('article', new Article()));
        return $table;
    }

    /**
     * @return ArticleCategoryTable
     */
    public static function getArticleCategoryTable(){
        $table = new ArticleCategoryTable();
        $table->setTableGateway(self::getGenericTableGateway('article_category', new ArticleCategory()));
        return $table;
    }


    /**
     * @return ArticleTagTable
     */
    public static function getArticleTagTable(){
        $table = new ArticleTagTable();
        $table->setTableGateway(self::getGenericTableGateway('article_tag', new ArticleTag()));
        return $table;
    }

    /**
     * @return TagTable
     */
    public static function getTagTable($lang){
        $table = new TagTable();
        $table->setTableGateway(self::getGenericTableGateway('tag', new Tag()));
        $table->setLang($lang);
        return $table;
    }

    /**
     * @return CategoryTable
     */
    public static function getCategoryTable($lang){
        $table = new CategoryTable();
        $table->setTableGateway(self::getGenericTableGateway('category', new Category()));
        $table->setLang($lang);
        return $table;
    }


    /**
     * @var DishTable
     */
    private static $dishTable;
    /**
     * @return DishTable
     */
    public static function getDishTable($lang){
        if(!self::$dishTable){
            $table = new DishTable();
            $table->setTableGateway(self::getGenericTableGateway('dish', new Dish()));
            self::$dishTable = $table;
        }
        self::$dishTable->setLang($lang);
        return self::$dishTable;
    }

    /**
     * @var DishLangTable
     */
    private static $dishLangTable;
    /**
     * @return DishLangTable
     */
    public static function getDishLangTable($lang){
        if(!self::$dishLangTable){
            $table = new DishLangTable();
            $table->setTableGateway(self::getGenericTableGateway('dish_lang', new DishLang()));
            self::$dishLangTable = $table;
        }
        self::$dishLangTable->setLang($lang);
        return self::$dishLangTable;
    }

    /**
     * @return DishCategoryTable
     */
    public static function getDishCategoryTable(){
        $table = new DishCategoryTable();
        $table->setTableGateway(self::getGenericTableGateway('dish_category', new DishCategory()));
        return $table;
    }

    /**
     * @return UnitTable
     */
    public static function getUnitTable($lang){
        $table = new UnitTable();
        $table->setTableGateway(self::getGenericTableGateway('unit', new Unit()));
        $table->setLang($lang);
        return $table;
    }

    /**
     * @return UnitLangTable
     */
    public static function getUnitLangTable(){
        $table = new UnitLangTable();
        $table->setTableGateway(self::getGenericTableGateway('unit_lang', new UnitLang()));
        return $table;
    }

    /**
     * @return DishTagTable
     */
    public static function getDishTagTable($lang){
        $table = new DishTagTable();
        $table->setTableGateway(self::getGenericTableGateway('dish_tag', new DishTag()));
        $table->setLang($lang);
        return $table;
    }

    /**
     * @return DishPriceTable
     */
    public static function getDishPriceTable(){
        $table = new DishPriceTable();
        $table->setTableGateway(self::getGenericTableGateway('dish_price', new DishPrice()));
        return $table;
    }

    /**
     * @return DishArticleTable
     */
    public static function getDishArticleTable($lang){
        $table = new DishArticleTable();
        $table->setTableGateway(self::getGenericTableGateway('dish_article', new DishArticle()));
        $table->setLang($lang);
        return $table;
    }

    /**
     * @return DishImageTable
     */
    public static function getDishImageTable(){
        $table = new DishImageTable();
        $table->setTableGateway(self::getGenericTableGateway('dish_image', new DishImage()));
        return $table;
    }

    /**
     * @return AddressTable
     */
    public static function getAddressTable(){
        $table = new AddressTable();
        $table->setTableGateway(self::getGenericTableGateway('address', new Address()));
        return $table;
    }

    /**
     * @return ProvinceTable
     */
    public static function getProvinceTable(){
        $table = new ProvinceTable();
        $table->setTableGateway(self::getGenericTableGateway('province', new Province()));
        return $table;
    }


    /**
     * @return DistrictTable
     */
    public static function getDistrictTable(){
        $table = new DistrictTable();
        $table->setTableGateway(self::getGenericTableGateway('district', new District()));
        return $table;
    }

    /**
     * @return RestaurantTable
     */
    public static function getRestaurantTable(){
        $table = new RestaurantTable();
        $table->setTableGateway(self::getGenericTableGateway('restaurant', new Restaurant()));
        return $table;
    }


    /**
     * @return RestaurantOwnerTable
     */
    public static function getRestaurantOwnerTable(){
        $table = new RestaurantOwnerTable();
        $table->setTableGateway(self::getGenericTableGateway('restaurant_owner', new RestaurantOwner()));
        return $table;
    }


    /**
     * @return DomainConfigTable
     */
    public static function getDomainConfigTable(){
        $table = new DomainConfigTable();
        $table->setTableGateway(self::getGenericTableGateway('domain_config', new DomainConfig()));
        return $table;
    }

    /**
     * @return DomainTemplateGroupTable
     */
    public static function getDomainTemplateGroupTable(){
        $table = new DomainTemplateGroupTable();
        $table->setTableGateway(self::getGenericTableGateway('domain_template_group', new DomainTemplateGroup()));
        return $table;
    }

    /**
     * @return DomainTemplateTable
     */
    public static function getDomainTemplateTable(){
        $table = new DomainTemplateTable();
        $table->setTableGateway(self::getGenericTableGateway('domain_template', new DomainTemplate()));
        return $table;
    }


    /**
     * @return DomainPageTable
     */
    public static function getDomainPageTable(){
        $table = new DomainPageTable();
        $table->setTableGateway(self::getGenericTableGateway('domain_page', new DomainPage()));
        return $table;
    }

    /**
     * @return DomainUserTable
     */
    public static function getDomainUserTable(){
        $table = new DomainUserTable();
        $table->setTableGateway(self::getGenericTableGateway('domain_user', new DomainUser()));
        return $table;
    }

    /**
     * @return UserBalanceTable
     */
    public static function getUserBalanceTable(){
        $table = new UserBalanceTable();
        $table->setTableGateway(self::getGenericTableGateway('user_balance', new UserBalance()));
        return $table;
    }




}

